package com.example.mqttmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mqttmanage.config.security.SecurityUtils;
import com.example.mqttmanage.domain.base.ResponseResult;
import com.example.mqttmanage.domain.device.Device;
import com.example.mqttmanage.mapper.DeviceMapper;
import com.example.mqttmanage.service.IDeviceService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    @Override
    public ResponseResult<Page<Device>> selectPageDevice(Long pageNum, Long pageSize, Device device) {
        // 创建分页对象
        Page<Device> page = new Page<>(pageNum, pageSize);
        // 构建查询条件
        LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
//        if (device != null) {
//            // 根据需要添加查询条件，例如：
//            queryWrapper.eq(device.getId() != null, Device::getId, device.getId())
//                        .like(device.getDeviceId() != null, Device::getDeviceId, device.getDeviceId())
//                        .like(device.getDeviceName() != null, Device::getDeviceName, device.getDeviceName())
//                        .like(device.getDeviceType() != null, Device::getDeviceType, device.getDeviceType())
//                        .like(device.getDeviceModel() != null, Device::getDeviceModel, device.getDeviceModel())
//                        .like(device.getFirmwareVersion() != null, Device::getFirmwareVersion, device.getFirmwareVersion())
//                        .eq(device.getStatus() != null, Device::getStatus, device.getStatus())
//                        .like(device.getMqttClientId() != null, Device::getMqttClientId, device.getMqttClientId())
//                        .like(device.getMqttUsername() != null, Device::getMqttUsername, device.getMqttUsername())
//                        .like(device.getTopicPrefix() != null, Device::getTopicPrefix, device.getTopicPrefix());
//        }

        // 执行分页查询
        Page<Device> devicePage = this.page(page, queryWrapper);
        return ResponseResult.success(devicePage);
    }

    @Override
    public ResponseResult<Device> insertDevice(Device device) {
        // 预先校验唯一性
        if (device.getMqttClientId() != null) {
            LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Device::getMqttClientId, device.getMqttClientId());
            if (this.count(queryWrapper) > 0) {
                return ResponseResult.error("MQTT客户端ID已存在，请使用其他ID");
            }
        }

        if (device.getDeviceId() != null) {
            LambdaQueryWrapper<Device> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Device::getDeviceId, device.getDeviceId());
            if (this.count(queryWrapper) > 0) {
                return ResponseResult.error("设备ID已存在，请使用其他ID");
            }
        }

        try {
            boolean saveResult = this.save(device);
            if (saveResult) {
                System.out.println("测试登录用户名" + SecurityUtils.getCurrentUsername());
                return ResponseResult.success(device);
            } else {
                return ResponseResult.error("设备添加失败");
            }
        } catch (Exception e) {
            return ResponseResult.error("设备添加失败: " + e.getMessage());
        }
    }


    @Override
    public ResponseResult<Device> selectById(Long id) {
        Device device = this.getById(id);
        if (device != null) {
            return ResponseResult.success(device);
        } else {
            return ResponseResult.error("设备不存在");
        }
    }

    @Override
    public ResponseResult<Device> updateDevice(Device device) {
        // 检查 ID 是否存在
        if (device.getId() == null || this.getById(device.getId()) == null) {
            return ResponseResult.error("设备不存在，无法更新");
        }


        boolean updateResult = this.updateById(device);
        if (updateResult) {
            return ResponseResult.success(device);
        } else {
            return ResponseResult.error("设备更新失败");
        }
    }

    @Override
    public ResponseResult<String> deleteById(Long id) {
        // 检查 ID 是否存在
        if (this.getById(id) == null) {
            return ResponseResult.error("设备不存在，无法删除");
        }

        // 可以在这里添加业务逻辑，例如检查是否有关联数据等
        // ... 业务校验 ...

        boolean deleteResult = this.removeById(id);
        if (deleteResult) {
            return ResponseResult.success("设备删除成功");
        } else {
            return ResponseResult.error("设备删除失败");
        }
    }

    @Override
    public ResponseResult<String> deleteBatch(List<Long> ids) {
        // 可以在这里添加业务逻辑，例如检查是否有关联数据等
        // ... 业务校验 ...

        boolean deleteResult = this.removeBatchByIds(ids);
        if (deleteResult) {
            return ResponseResult.success("设备批量删除成功");
        } else {
            return ResponseResult.error("设备批量删除失败");
        }
    }
}